package com.app.mymusic.ui.charts;

import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.app.mymusic.R;
import com.app.mymusic.adapter.ChartsAdapter;
import com.app.mymusic.adapter.MyViewHolder;
import com.app.mymusic.adapter.SampleAdapter;
import com.app.mymusic.model.Songs;
import com.app.mymusic.utils.LogUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChartsActivity extends AppCompatActivity {
    @Inject
    ChartsPresenter presenter;

    @BindView(R.id.charts_rv)
    RecyclerView chartsRv;
    @BindView(R.id.charts_cl)
    CoordinatorLayout chartsCl;
    @BindView(R.id.activity_charts)
    SwipeRefreshLayout activityCharts;
    private ChartsAdapter mAdapter;

    private SampleAdapter sampleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_charts);
        ButterKnife.bind(this);
        DaggerChartsComponent.builder()
                .chartsModule(new ChartsModule(ChartsActivity.this))
                .build()
                .inject(this);
        presenter.getCharts("","");
        activityCharts.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                 /*   new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(3000);
                                activityCharts.setRefreshing(false);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();*/
                activityCharts.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        activityCharts.setRefreshing(false);
                    }
                });
            }
        });

    }

    public void showRes(Songs songs){
        LogUtil.showLog("num:"+songs.getSong_list().size());
        sampleAdapter=new SampleAdapter<Songs.Song_list>(songs.getSong_list(),ChartsActivity.this) {

            @Override
            public void convert(MyViewHolder holder, Songs.Song_list song) {
                holder.setText(R.id.song_name,song.getTitle());
            }
        };
        chartsRv.setLayoutManager(new LinearLayoutManager(this));
        chartsRv.setAdapter(sampleAdapter);
        /****/
       /* mAdapter= new ChartsAdapter(R.layout.item_charts, songs.getSong_list()) {
            @Override
            public void getBitmap(Songs.Song_list song, final BaseViewHolder baseViewHolder) {
                LogUtil.showLog("getBitmap:------------"+song.getPic_small());
                Glide.with(ChartsActivity.this).load(Uri.parse(song.getPic_small()))
                       .into(new SimpleTarget<GlideDrawable>() {
                           @Override
                           public void onLoadStarted(Drawable placeholder) {}

                           @Override
                           public void onLoadFailed(Exception e, Drawable errorDrawable) {}

                           @Override
                           public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {

                               baseViewHolder.setImageDrawable(R.id.charts_img,resource);
                           }

                           @Override
                           public void onLoadCleared(Drawable placeholder) {}

                           @Override
                           public void setRequest(Request request) {}

                           @Override
                           public Request getRequest() {
                               return null;
                           }

                           @Override
                           public void onStart() {}

                           @Override
                           public void onStop() {}

                           @Override
                           public void onDestroy() {}
                       });
            }
        };
        chartsRv.setLayoutManager(new LinearLayoutManager(this));
        chartsRv.setAdapter(mAdapter);*/

    }
    public void showError(){
        Toast.makeText(this, R.string.no_datas, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}
